﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace 玩具商城管理系统
{
    public partial class Torder : Form
    {

        Class1 shopcarSrv = new Class1();
        public Torder()
        {
            InitializeComponent();
        }
        

        private void label5_Click(object sender, EventArgs e)
        {

        }
        public static decimal profit = 0;
        SqlDataAdapter dashopcar, daProduct;
        DataSet ds = new DataSet();
        void init()
        {
            string str = "select shopcar_id,shopcar_commodityid,shopcar_clientid,shopcar_commodityname,shopcar_money,shopcar_num from shopcar where shopcar_clientid='" + login.username + "'";
            dashopcar = new SqlDataAdapter(str, DB.cn);
            dashopcar.Fill(ds, "shopcar_info");
            string sdr = "select * from commodity";
            daProduct = new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds, "product_info");
        }
        void Bind()
        {
            init();
            
            profit = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item2;
            DataView dvCartItem = new DataView(ds.Tables["shopcar_info"]);
            dgvProduct.DataSource = dvCartItem;  
        }
        void Clear_shopcar()//清空购物车
        {
            List<int> List_index = new List<int>();
            if (dgvProduct.Rows.Count > 0)
            {
                for (int i = 0; i < dgvProduct.Rows.Count - 1; i++)
                {
                    List_index.Add(i);

                }
                int z = 0;
                foreach (int n in List_index)
                {
                    dgvProduct.Rows.RemoveAt(n - z);
                    z++;
                }
                UpdateDB();
            }
        }
        void UpdateDB()//更新数据源
        {
            try
            {
                SqlCommandBuilder dbshopcar = new SqlCommandBuilder(dashopcar);
                dashopcar.Update(ds, "shopcar_info");
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                daProduct.Update(ds, "product_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Torder_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“toyStroeDataSet.shopcar”中。您可以根据需要移动或移除它。
            this.shopcarTableAdapter.Fill(this.toyStroeDataSet.shopcar);
            DB.GetCn();
            string sdr = "select client_power from client where client_name='" + login.username + "'";
            SqlCommand command = new SqlCommand(sdr, DB.cn);
            // 执行查询并获取结果
            object result = command.ExecuteScalar();
            string client_Power = (result != null) ? result.ToString() : "";
            if (client_Power == "高级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label9.Text = discountedPrice.ToString();
                
            }
            else if(client_Power == "初级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label9.Text = discountedPrice.ToString();
            }
            else
            {
                label9.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
            }
            label13.Parent = label12;
            Bind();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtaddress.Text == "" || txtcity.Text == "" || txtcode.Text == "" || txtcovence.Text == "" ||txtphone.Text=="")
            {
                MessageBox.Show("亲亲所有项都需要填，这样子我们才能更好的为你配送商品");
            }
            else//添加订单
            {
                DB.GetCn();
                string str = "insert into orderT values('" + login.username + "','" + DateTime.Now + "','" + txtaddress.Text + "','"
                   + txtcity.Text + "','" + txtcovence.Text + "','" + txtcode.Text + "','" + txtphone.Text
                   + "','未审核',null," + Tshopcar.profit + ",'暂无备注',null,null)";
                Tshopcar.profit = 0;
                DB.sqlEx(str);
                for (int i = 0; i < dgvProduct.Rows.Count; i++)//添加出售商品数量HOT表
                {
                   if (dgvProduct.Rows[i].Cells["shopcar_commodityname"].Value != null)
                    {
                        DB.GetCn();
                        string str2 = "";
                        string cd = "";
                        string num = "";
                        string id = dgvProduct.Rows[i].Cells["shopcar_id"].Value.ToString();
                        cd = dgvProduct.Rows[i].Cells["shopcar_commodityname"].Value.ToString();
                        num= dgvProduct.Rows[i].Cells["shopcar_num"].Value.ToString();
                        str2 = "insert into hot values('" + DateTime.Now +"-"+ cd + "','" + cd + "','" + num + "','等待审核','"+ id +"','"+ DateTime.Now + "','未完成','"+login.username+"', null,null,null,null,null,null,null,null,null)";
                        DB.sqlEx(str2);
                        
                    }
                }
                //HOT表获取订单号
                DB.GetCn();
                string orderid = "";
                string hot_orderid = "";
                orderid = "select MAX(orderT_id) from orderT";
                SqlCommand cmd =new SqlCommand(orderid, DB.cn);
                object a = cmd.ExecuteScalar();
                string b = (a != null) ? a.ToString() : "";
                hot_orderid = "update hot set orderT_id="+b+ " where hot_time = (select MAX(hot_time) from hot)";
                DB.sqlEx(hot_orderid);
                Clear_shopcar();
                this.Close();
            }
            DB.GetCn();
            string sdr = "select client_power from client where client_name='" + login.username + "'";
            SqlCommand command = new SqlCommand(sdr, DB.cn);
            // 执行查询并获取结果
            object result = command.ExecuteScalar();
            string client_Power = (result != null) ? result.ToString() : "";
            string pro = "";
            if (client_Power == "高级用户")
            {
                 pro = "update orderT set profit=round(profit*0.8, 2) where orderT_id = (select MAX(orderT_id) from orderT);";
            }
            else if (client_Power == "初级用户")
            {

                 pro = "update orderT set profit=round(profit*0.9, 2) where orderT_id = (select MAX(orderT_id) from orderT)";

            }
            else
            {
                 pro = "update orderT set profit=profit where orderT_cid='" + login.username + "'";               
            }

            // 执行更新操作
            DB.sqlEx(pro);
            MessageBox.Show("已经成功结算,欢迎再次光临");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您真的要取消结算吗？现在商品是优惠时间段", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                
                this.Close();
            }
            else
            {
                return;
            }
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            people pp = new people();
            pp.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label13.Top = label12.Height/2 ;
            if (label13.Left < -label12.Width)
            {
                label13.Left = label12.Width;
            }
            label13.Left -= 10;
        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }
    }
}
